import { AxiosRequestConfig } from 'axios'
import { Toast, Dialog } from 'vant'
import service from './axios'
interface BaseResponse<T> {
  code: number
  status: boolean
  data: T
  message?: string
}

const request = <T>(config: AxiosRequestConfig): Promise<BaseResponse<T>> => {
  return new Promise((resolve, reject) => {
    service.request<BaseResponse<T>>(config).then(
      (res) => {
        resolve(res.data)
      },
      async (err) => {
        switch (err?.code) {
          case 401:
            // token失效
            await Dialog.alert({
              title: '提示',
              message: '您还未登录或登录已过期，请重新登录'
            })
            window.location.href = "#/login"
            break
          default:
            Toast(err.msg)
            break
        }
        reject(err)
      }
    )
  })
}

export default request
